package com.hbnu.dao;

import com.hbnu.entity.StudyRecord;
import com.hbnu.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class StudyRecordDAO {
    // 查询用户待复习的单词（当天及已过期）
    public List<StudyRecord> getReviewRecords(int userId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<StudyRecord> records = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            // 查询下次复习时间 <= 今天的记录
            String sql = "SELECT * FROM study_record WHERE user_id = ? AND next_review_time <= CURDATE()";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, userId);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                StudyRecord record = new StudyRecord();
                record.setId(rs.getInt("id"));
                record.setUserId(rs.getInt("user_id"));
                record.setWordId(rs.getInt("word_id"));
                record.setFamiliarity(rs.getInt("familiarity"));
                record.setNextReviewTime(rs.getDate("next_review_time"));
                record.setReviewCount(rs.getInt("review_count"));
                records.add(record);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
//            DBUtil.close(conn, pstmt, rs);
        }
        return records;
    }

    // 更新学习记录
    public void updateStudyRecord(int userId, int wordId, int familiarity) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            // 先查询是否存在记录
            String checkSql = "SELECT id FROM study_record WHERE user_id = ? AND word_id = ?";
            pstmt = conn.prepareStatement(checkSql);
            pstmt.setInt(1, userId);
            pstmt.setInt(2, wordId);
            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                // 存在则更新
                String updateSql = "UPDATE study_record SET familiarity = ?, review_count = review_count + 1, " +
                        "next_review_time = DATE_ADD(CURDATE(), INTERVAL ? DAY) WHERE user_id = ? AND word_id = ?";
                pstmt = conn.prepareStatement(updateSql);
                pstmt.setInt(1, familiarity);
                pstmt.setInt(2, getIntervalDays(familiarity)); // 根据熟悉度计算下次                pstmt.setInt(3, userId);
                pstmt.setInt(4, wordId);
                pstmt.executeUpdate();
            } else {
                // 不存在则插入
                String insertSql = "INSERT INTO study_record (user_id, word_id, familiarity, review_count, next_review_time) " +
                        "VALUES (?, ?, ?, 1, DATE_ADD(CURDATE(), INTERVAL ? DAY))";
                pstmt = conn.prepareStatement(insertSql);
                pstmt.setInt(1, userId);
                pstmt.setInt(2, wordId);
                pstmt.setInt(3, familiarity);
                pstmt.setInt(4, getIntervalDays(familiarity));
                pstmt.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
//            DBUtil.close(conn, pstmt, null);
        }
    }

    // 根据熟悉度计算下次复习间隔（艾宾浩斯记忆法简化版）
    private int getIntervalDays(int familiarity) {
        switch (familiarity) {
            case 0: return 1;   // 陌生：1天后复习
            case 1: return 2;   // 认识：2天后复习
            case 2: return 4;   // 熟悉：4天后复习
            case 3: return 7;   // 较熟：7天后复习
            case 4: return 15;  // 熟练：15天后复习
            case 5: return 30;  // 掌握：30天后复习
            default: return 1;
        }
    }
}